import persistent from '../common/persistentUtil'

export default {


    loadAuthInfo(router) {

        window.axios.get("/auth/getAuthInfo")
            .then(d => {

                if (d.data.status === 0) {
                    let authInfo = d.data.data;
                    persistent.saveAuthenticationInfo(authInfo);
                    router.replace("/");
                } else {
                    throw "获取授权信息出错";
                }

            })
            .catch(e => {
                throw e;
            })

    },

    login(username, password,router, s, e) {

        let params = new URLSearchParams();
        params.append("username", username);
        params.append("password", password);
        window.axios.post("/auth/login", params).then(d => {

            let result = d.data;

            if (result.status === 0) {

                let token = result.data;
                if (s) {
                    s();
                }
                persistent.saveToken(token);

                //清除本地字典信息
                persistent.clearDicts();
                //登录后获取授权信息
                this.loadAuthInfo(router);
                // persistent.saveAuthenticationInfo(authInfo);

            } else {

                if (e) {
                    e("用户名或密码错误");
                }
            }

        })
            .catch(e1 => {

                if (e) {
                    e("服务器错误:" + e1.message);
                }
            })
    }
}
